<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商铺" prop="code">
        <!-- <el-select v-model="queryParams.shopsId" placeholder="请选择商铺" clearable>
          <el-option v-for="dict in shopsList" :key="dict.id" :label="dict.code" :value="dict.id" />
        </el-select> -->
        <HouseInput v-model="queryParams.code" type="house" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-form :model="shops" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item>
        <div class="myDiv">
          <div style="width:200px">编号：<span>{{ cashregisterList[0].code }}</span></div>

          <div>商铺状态：
            <span v-if="cashregisterList[0].zhwyShops != null">
              <span v-if="cashregisterList[0].zhwyShops.state == 0">空置</span>
              <span v-else-if="cashregisterList[0].zhwyShops.state == 1">已出租</span>
              <span v-else-if="cashregisterList[0].zhwyShops.state == 2">已出售</span>
              <span v-else-if="cashregisterList[0].zhwyShops.state == 3">即将到期</span>
              <span v-else-if="cashregisterList[0].zhwyShops.state == 4">租约预期</span>
            </span>

          </div>

          <div>占地面积：
            <span v-if="cashregisterList[0].zhwyShops != null">
              <span>{{ cashregisterList[0].zhwyShops.floorArea }}</span>
            </span>
          </div>
        </div>
        <div class="myDiv">
          <div>使用面积：
            <span v-if="cashregisterList[0].zhwyShops != null">
              <span>{{ cashregisterList[0].zhwyShops.useArea }}</span>
            </span>
          </div>

          <!-- （租售合同） -->
          <div>行业：
            <span v-if="cashregisterList[0].zhwyRentalcontract != null">
              <span>{{ cashregisterList[0].zhwyRentalcontract.trade }}</span>
            </span>
          </div>
          <!-- （租户信息） -->
          <div style="width:200px">客户姓名：
            <span v-if="cashregisterList[0].zhwyHirer != null">
              <span>{{ cashregisterList[0].zhwyHirer.name }}</span>
            </span>
          </div>
        </div>

        <div class="myDiv">
          <!-- (租户信息) -->
          <div>联系方式：
            <span v-if="cashregisterList[0].zhwyHirer != null">
              <span>{{ cashregisterList[0].zhwyHirer.phone }}</span>
            </span>
          </div>

        </div>
      </el-form-item>
    </el-form>


    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="常规收费" name="first">
        常规收费
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
              v-hasPermi="['msr:cashregister:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['msr:cashregister:remove']">删除</el-button>
          </el-col>

          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="cashregisterList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" align="center" prop="zhwyChargedata.id" />
          <!-- 7收费项目表 -->
          <el-table-column label="收费项名称" align="center" prop="zhwyChargeitem.name" />
          <!-- 13收费数据表 -->
          <el-table-column label="起收日期" align="center" prop="zhwyChargedata.startTime" />
          <el-table-column label="到期日期" align="center" prop="zhwyChargedata.endTime" />
          <el-table-column label="缴费期限" align="center" prop="zhwyChargedata.deadlineTime" />
          <el-table-column label="单价" align="center" prop="zhwyChargedata.univalence" />
          <el-table-column label="数量" align="center" prop="zhwyChargedata.number" />
          <el-table-column label="金额" align="center" prop="zhwyChargedata.money" />

          <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['msr:cashregister:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['msr:cashregister:remove']">删除</el-button>
            </template>
</el-table-column> -->
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改综合收银对话框 -->
        <el-dialog :title="title" :visible.sync="open2" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>

      </el-tab-pane>
      <el-tab-pane label="临时收费/收取押金" name="second">
        临时收费/收取押金
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd2"
              v-hasPermi="['msr:cashregister:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['msr:cashregister:remove']">删除</el-button>
          </el-col>

          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="cashregisterList[0].zhwyTemporarydepositList"
          @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" align="center" prop="id" />
          <!-- name -->
          <el-table-column label="客户姓名" align="center" prop="name" />
          <!-- 7收费项目表 -->
          <el-table-column label="收费项目" align="center" prop="chargeItemName" />
          <!-- 12支付方式 -->
          <el-table-column label="付款方式" align="center" prop="paymentMethodName" />
          <!-- 14临时收费/押金管理 -->
          <el-table-column label="收费金额" align="center" prop="money" />
          <el-table-column label="收款人" align="center" prop="chargeUser" />
          <el-table-column label="收款时间" align="center" prop="chargeTime" />
          <!-- 14临时收费/押金管理 -->
          <!-- <el-table-column label="状态" align="center" prop="state" /> -->
          <el-table-column label="状态" align="center" prop="state">
            <template slot-scope="scope">
              <!-- <dict-tag :options="dict.type.device_status" :value="scope.row.state" /> -->
              <el-button v-if="scope.row.state == 0" size="mini"
                style="background-color:blue;color:white">已付款</el-button>
              <el-button v-else-if="scope.row.state == 1" size="mini"
                style="background-color:blue;color:white">未付款</el-button>

            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="chargeRemark" />

          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['msr:cashregister:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['msr:cashregister:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改综合收银对话框 -->
        <el-dialog :title="title" :visible.sync="open2" width="500px" append-to-body>
          <el-form ref="form" :model="form2" :rules="rules" label-width="80px">
            <el-form-item label="编号" prop="shopsId">
              <el-input v-model="form2.code" placeholder="请输入商铺编号" disabled />
            </el-form-item>
            <el-form-item label="客户姓名" prop="name">
              <el-input v-model="form2.name" placeholder="请输入客户姓名" disabled />
            </el-form-item>
            <el-form-item label="收费项目" prop="chargeltemId">
              <el-select v-model="form2.chargeltemId" placeholder="请选择" clearable>
                <el-option v-for="dict in chargeitemList" :key="dict.id" :label="dict.name" :value="dict.id" />
              </el-select>
            </el-form-item>
            <el-form-item label="付款方式" prop="payId">
              <el-select v-model="form2.payId" placeholder="请选择状态" clearable>
                <el-option v-for="dict in paymentmethodList" :key="dict.id" :label="dict.name" :value="dict.id" />
              </el-select>
            </el-form-item>
            <el-form-item label="收费金额" prop="money">
              <el-input v-model="form2.money" placeholder="请输入收费金额" />
            </el-form-item>
            <el-form-item label="收款时间" prop="chargeTime">
              <el-date-picker clearable v-model="form2.chargeTime" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择收款时间">
              </el-date-picker>
            </el-form-item>

          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm2">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-tab-pane>
      <!-- <el-tab-pane label="收取押金" name="third">收取押金</el-tab-pane> -->


      <el-tab-pane label="预存款" name="fourth">
        预存款
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
              v-hasPermi="['msr:cashregister:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['msr:cashregister:remove']">删除</el-button>
          </el-col>

          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="cashregisterList[0].zhwyAdvancedepositList"
          @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" align="center" prop="id" />
          <!-- name -->
          <el-table-column label="客户姓名" align="center" prop="name" />
          <el-table-column label="状态" align="center" prop="state">
            <template slot-scope="scope">
              <!-- <dict-tag :options="dict.type.device_status" :value="scope.row.state" /> -->
              <el-button v-if="scope.row.state == 0" size="mini"
                style="background-color:blue;color:white">预存</el-button>
              <el-button v-else-if="scope.row.state == 1" size="mini"
                style="background-color:blue;color:white">支付</el-button>
              <el-button v-else-if="scope.row.state == 2" size="mini"
                style="background-color:blue;color:white">退还</el-button>
              <el-button v-else-if="scope.row.state == 3" size="mini"
                style="background-color:blue;color:white">找零结转</el-button>

            </template>
          </el-table-column>

          <el-table-column label="收费项目" align="center" prop="chargeItemName" />

          <el-table-column label="付款方式" align="center" prop="paymentMethodName" />
          <!-- 14预存款 -->
          <el-table-column label="金额" align="center" prop="amt" />
          <el-table-column label="收款人" align="center" prop="chargeUser" />
          <el-table-column label="收款时间" align="center" prop="payTime" />
          <!-- 13收费数据表 -->
          <!-- <el-table-column label="单号" align="center" prop="chargeTime" /> -->

          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['msr:cashregister:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['msr:cashregister:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改综合收银对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-tab-pane>
    </el-tabs>



  </div>
</template>

<script>
import { listCashregister, getCashregister, delCashregister, addCashregister, updateCashregister } from "@/api/msr/cashregister";
import { listChargeitem, getChargeitem, delChargeitem, addChargeitem, updateChargeitem } from "@/api/hyj/chargeitem";
import { listShops, getShops } from "@/api/system/shops";
import { listPaymentmethod, getPaymentmethod, delPaymentmethod, addPaymentmethod, updatePaymentmethod } from "@/api/msr/paymentmethod";
import {
  addTemporarydeposit,
  delTemporarydeposit,
  listTemporarydeposit,
  updateTemporarydeposit
} from "@/api/zxwy/temporarydeposit";
export default {
  name: "Cashregister",
  data() {
    return {
      // 支付方式表格数据
      paymentmethodList: [],
      // 收费项目管理表格数据
      chargeitemList: [],
      activeName: 'first',
      //商铺列表
      shopsList: [],
      //商铺对象
      shops: {},

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 综合收银表格数据
      cashregisterList: [{
        code: null,
        zhwyShops: {
          state: null,
          floorArea: null,
        },
        zhwyRentalcontract: {
          trade: null
        },
        zhwyHirer: {
          name: null,
          phone: null
        }

      }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null
      },
      // 表单参数
      form: {},
      form2: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event);
    },
    /** 查询综合收银列表 */
    getList() {
      this.loading = true;
      listCashregister(this.queryParams).then(response => {
        this.cashregisterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      /** 查询商铺信息管理列表 */
      listShops().then(response => {
        this.shopsList = response.rows;
      });
      /** 查询收费项目管理列表 */
      listChargeitem(this.queryParams).then(response => {
        this.chargeitemList = response.rows;

      });
      /** 查询支付方式列表 */
      listPaymentmethod(this.queryParams).then(response => {
        this.paymentmethodList = response.rows;

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加综合收银";
    },
    handleAdd2() {
      this.reset();
      this.open2 = true;
      this.title = "新建临时收费";
      this.form2.code = this.cashregisterList[0].code
      this.form2.name = this.cashregisterList[0].zhwyHirer.name
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCashregister(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改综合收银";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCashregister(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCashregister(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm2() {
      this.form2.shopsId=this.cashregisterList[0].zhwyShops.id
      this.form2.chargeItemId=this.cashregisterList[0].zhwyChargeitem.id
          if (this.form2.id != null) {
            updateTemporarydeposit(this.form2).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open2 = false;
              this.getList();
            });
          } else {
            addTemporarydeposit(this.form2).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open2 = false;
              this.getList();
            });
          }
       
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除综合收银编号为"' + ids + '"的数据项？').then(function () {
        return delCashregister(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('msr/cashregister/export', {
        ...this.queryParams
      }, `cashregister_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.myDiv {
  margin-left: 20px;
}

.myDiv div {
  display: inline-block;
  width: 200px;
  font-weight: bold;
}

.myDiv span {
  font-weight: bold;
  color: blueviolet;
}
</style>
